package org.hw.conifg;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket docket(){
        List<Parameter> parameters = new ArrayList<>();
        //这边默认都增加token，但是设置非必填，因为不是每个接口都需要该参数
        parameters.add(new ParameterBuilder()
                .name("token")
                .description("认证token")
                .modelRef(new ModelRef("string"))
                .parameterType("header")
                .required(false)
                .build());
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .enable(true)
                .select()
                .apis(RequestHandlerSelectors.basePackage("org.hw"))
                .paths(PathSelectors.any())
                .build()
                .globalOperationParameters(parameters);
    }

    /**
     * 信息
     * @return
     */
    private ApiInfo apiInfo() {
        //作者信息
        Contact contact = new Contact("guyang","https://www.baidu.com","503646110@qq.com");
        return new ApiInfo("hw java",
                "hw java 接口文档",
                "1.0",
                "https://www.baidu.com",
                contact,
                "Apache 2.0",
                "https://www.baidu.com",
                new ArrayList());
    }

}

